<?php
 
namespace app\common;
 
use OSS\OssClient;
use OSS\Core\OssException;
 
/**
 * @note OSS上传操作
 */
class AliOss
{
    protected $accessKeyId = '';
    protected $accessKeySecret = '';
    protected $endpoint = '';
    protected $bucket = '';
   
   
   public function __construct()
    {
        $this->accessKeyId = '';//id
        $this->accessKeySecret = '';//秘钥
        $this->endpoint = '';//域名
        $this->bucket = '';//桶名
    }
   
   //上传
  public static function upload($filePath, $object = null)
    {
        try {
            $ossClient = new OssClient('LTAI5tSgVr2zwJ8qba9E5oVh', 'wO5MwGoogYIXrFfwGoPSMUfzMUvjlH', 'oss-cn-beijing.aliyuncs.com');
            if ($object === null) {
                $object = pathinfo($filePath, PATHINFO_BASENAME);
            }
            $result = $ossClient->uploadFile('lanshifu', $object, $filePath);
            return $result['info']['url'];
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return false;
        }
    }
    
    //下面代码可以直接用
    // $files = $_FILES['file'];
    // $name = $files['name'];
    // $format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
    // $fileUrl = $files["tmp_name"];
    // $object = 'trip/'.date("Ymd")."/".sha1(date('YmdHis', time()) . uniqid()) . $format;
    // $res = AliOss::upload($fileUrl, $object);
 
}